package com.business_clean.ui.fragment.addproject;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.lifecycle.Observer;

import com.business_clean.app.App;
import com.business_clean.app.base.BaseFragment;
import com.business_clean.app.callback.OnDialogSelectListener;
import com.business_clean.app.config.ConstantMMVK;
import com.business_clean.app.ext.CommonUtils;
import com.business_clean.app.uitl.MMKVHelper;
import com.business_clean.app.uitl.ToastUtil;
import com.business_clean.data.mode.init.BaseIDNameEntity;
import com.business_clean.data.mode.init.InitDataEntity;
import com.business_clean.data.mode.members.ContactEntity;
import com.business_clean.data.mode.members.IDCardMemberEntity;
import com.business_clean.data.mode.members.MembersDetailEntity;
import com.business_clean.databinding.FragmentContactBinding;
import com.business_clean.ui.activity.custom.AddProjectActivity;
import com.business_clean.viewmodel.request.ProjectMembersViewModel;
import com.google.gson.Gson;
import com.lxj.xpopup.interfaces.OnSelectListener;

import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ContactInformationFragment extends BaseFragment<ProjectMembersViewModel, FragmentContactBinding> {


    private String exigency_user_relation;
    private String uuid = "";
    private int type = 0;

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        getMDatabind().withContactPhone.setInputPhone();
        getMDatabind().withPhone.setInputPhone();
    }


    @Override
    public void lazyLoadData() {
        if (getArguments() != null) {//说明已经存在了
            uuid = getArguments().getString("uuid");
            type = getArguments().getInt("type");
            String application_no = getArguments().getString("application_no");

            HashMap<String, String> hashMap = new HashMap<>();
            if (!TextUtils.isEmpty(uuid)) {
                hashMap.put("uuid", uuid);
            }
            if (!TextUtils.isEmpty(application_no)) {
                hashMap.put("application_no", application_no);
            }
            hashMap.put("is_contact", "1");
            //1是草稿编辑  2是草稿新建 3是档案详情
            if (type == 3) {
                mViewModel.requestStaffDetail(hashMap);//真正的详情
            } else {
                mViewModel.requestMembersDetail(hashMap);//草稿的详情，还没真的成为员工
            }
        }
    }

    @Override
    public void initObserver() {
        App.appViewModelInstance.getCreateUuid().observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                uuid = s;
            }
        });
    }

    @Override
    public void onRequestSuccess() {
        mViewModel.getUpdateContactsStatus().observe(this, new Observer<IDCardMemberEntity>() {
            @Override
            public void onChanged(IDCardMemberEntity idCardMemberEntity) {
                App.appViewModelInstance.getProjectPersonnel().setValue(2);
            }
        });
        mViewModel.getMembersDetailEntity().observe(this, new Observer<MembersDetailEntity>() {
            @Override
            public void onChanged(MembersDetailEntity membersDetailEntity) {
                uuid = membersDetailEntity.getUuid();
                fillData(membersDetailEntity.getContact());
            }
        });
    }

    private void fillData(ContactEntity contact) {
        if (contact != null) {
            getMDatabind().withPhone.setInputText(contact.getMobile());
            getMDatabind().withContactPhone.setInputText(contact.getExigency_user_mobile());
            getMDatabind().withEmergencyContact.setInputText(contact.getExigency_user_name());
            getMDatabind().withEmergencyContactIdentity.setTvRightText(contact.getExigency_user_relation_name());
            exigency_user_relation = contact.getExigency_user_relation();
        }
    }

    @Override
    public void onBindViewClick() {
        getMDatabind().withEmergencyContactIdentity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (AddProjectActivity.initDataEntity != null && AddProjectActivity.initDataEntity.getExigency_user_relation_list().size() > 0) {
                    CommonUtils.showDialogWheelSingleList(getActivity(), "请选择紧急联系人身份", getArrayListString(AddProjectActivity.initDataEntity.getExigency_user_relation_list()), getMDatabind().withEmergencyContactIdentity.getTvRight(), new OnDialogSelectListener() {
                        @Override
                        public void onSelect(int position, String text) {
                            exigency_user_relation = AddProjectActivity.initDataEntity.getExigency_user_relation_list().get(position).getId();
                            getMDatabind().withEmergencyContactIdentity.setTvRightText(text);
                        }
                    });
                }
            }
        });

        getMDatabind().bottom.butBottomButLast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                App.appViewModelInstance.getProjectLastPersonnel().setValue(0);
            }
        });
        getMDatabind().bottom.butBottomButNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(getMDatabind().withPhone.getInputText())) {
                    ToastUtil.show("请输入本人电话");
                    return;
                }
                if (TextUtils.isEmpty(getMDatabind().withEmergencyContact.getInputText())) {
                    ToastUtil.show("请输入紧急联系人名称");
                    return;
                }
                if (TextUtils.isEmpty(getMDatabind().withContactPhone.getInputText())) {
                    ToastUtil.show("请输入紧急联系人电话");
                    return;
                }
                if (getMDatabind().withEmergencyContactIdentity.getTvRight().equals("请选择")) {
                    ToastUtil.show("请选择紧急联系人身份");
                    return;
                }

                HashMap<String, String> hashMap = new HashMap<>();
                hashMap.put("op_type", "contact");
                hashMap.put("uuid", uuid);
                hashMap.put("mobile", "" + getMDatabind().withPhone.getInputText());
                hashMap.put("exigency_user_name", "" + getMDatabind().withEmergencyContact.getInputText());
                hashMap.put("exigency_user_mobile", "" + getMDatabind().withContactPhone.getInputText());
                hashMap.put("exigency_user_relation", "" + exigency_user_relation);
                mViewModel.requestUpdateMembers(hashMap, AddProjectActivity.ID_CONTACTS, type);
            }
        });
    }


    private ArrayList<String> getArrayListString(List<BaseIDNameEntity> list) {
        ArrayList<String> names = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            names.add(list.get(i).getName());
        }
        return names;
    }
}
